package com.prim.threadcoreknowledge.stopthread;

/**
 * run方法阻塞方法时停止线程
 */
public class RightWayStopThreadWithSleep implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new RightWayStopThreadWithSleep());
        thread.start();
        Thread.sleep(500);
        thread.interrupt();//interrupt只是通知了线程中断，但是线程是否要中断取决于线程本身。
    }

    @Override
    public void run() {
        try {
            int num = 0;
            //Thread.currentThread().isInterrupted()加上线程响应中断
            while (!Thread.currentThread().isInterrupted() && num <= 30000) {
                if (num % 100 == 0) {
                    System.out.println(num + "是100的倍数");
                }
                num++;
            }

            Thread.sleep(1000);
            System.out.println("任务执行结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
